﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using SlimDX.Direct3D9;
using System.CodeDom.Compiler;

namespace GameStudio.CodeGeneration
{
    public class GenerateObject : Sample
    {

        public GenerateObject(string strFileName, string Path, string NameSpace)
            : base(strFileName, Path, NameSpace)
        {

            base.AssignObjects();
            samples.Imports.Add(new CodeNamespaceImport("SlimDX.Direct3D9"));
            samples.Imports.Add(new CodeNamespaceImport("System.Drawing"));

            base.Init();
            base.AddConstructor(null, null);
        }
        protected void AddMyFields()
        {
            CodeMemberField mf = base.AddFields("_Nodes", "SlimDX.Vector3[]");
            mf.Attributes = MemberAttributes.Public;
            targetClass.Members.Add(mf);
            //...........................................

            mf = base.AddFields("_CubeColor", "System.Drawing.Color[]");
            mf.Attributes = MemberAttributes.Public;
            targetClass.Members.Add(mf);
            //...........................................
            //CodeArrayCreateExpression ArrOfColor = new CodeArrayCreateExpression();
            //ArrOfColor.CreateType = new CodeTypeReference(typeof(System.Drawing.Color));

            //CodeSnippetStatement csu1 = new CodeSnippetStatement("_Triangles =");

            //constructor.Statements.Add(csu1);
            //constructor.Statements.Add(AssignArr);

        }
        private void InitializeArray(string Name,
                              params char[] Characters)
        {
            // Get a TypeReference for the Character 
            // array that was passed in
            // so that we can duplicate this data 
            // type in our generated code.
            CodeTypeReference tr = new CodeTypeReference(Characters.GetType());

            // Declare an array that matches our local array
            CodeVariableDeclarationStatement Declaration =
              new CodeVariableDeclarationStatement(tr, Name);

            // The CodePrimitiveExpression object is used to 
            // represent "primitive" or value data types such 
            // as char, int, double, etc.    We will use
            // an array of these primitive expressions to 
            // initialize the array we are declaring.
            CodePrimitiveExpression[] cpe = new
              CodePrimitiveExpression[Characters.Length];

            // Loop through our local array of characters, 
            // creating the objects
            // for our array of CodePrimitiveExpressions
            for (int i = 0; i < Name.Length; i++)
            {
                // Each CodePrimitiveExpression will have a language
                // independant representation of a character
                cpe[i] = new CodePrimitiveExpression(Characters[i]);
            }

            // The CodeArrayCreateExpression will handle calling 
            // the default constructor for the data type in the 
            // array.   Because we are also passing in the array of
            // CodePrimitiveExpressions, we won't need to specify 
            // the size of the array, and
            // every value in the array will have its initial value.
            CodeArrayCreateExpression array = new
              CodeArrayCreateExpression(tr, cpe);

            // Specify that this CodeArrayCreateExpression will 
            // initialize our array variable declartion
            Declaration.InitExpression = array;

            //return Declaration;
        }

        protected void AddMyProperties()
        {

            base.AddProperties("Nodes", "_Nodes", "SlimDX.Vector3[]");
        }

        protected void AddMyMethods()
        {
            string[] ArguTypes = { "SlimDX.Vector3[]", "Color[]" };
            string[,] Arr = { { "_Nodes", "_CubeColor" }, { "FilledMesh", "MyColours" } };
            CodeMemberMethod MyMethod = base.AddMethod("Initialize", Arr, ArguTypes);

            targetClass.Members.Add(MyMethod);
        }

        public void Generate(string file)
        {
            AddReferences();
            AddMyFields();
            AddMyProperties();
            //=========================
            AddMyMethods();
            //=========================
            if (file == null)
            {
                file = "VerySimpleCode.cs";
            }
            targetClass.Members.Add(constructor);
            
            CompilerResults result = base.GenerateCSharpCode();

            //if (result.Errors.HasErrors)
            //{
            //    Sample.DisplayCompilerResults(result);
            //}
            //else
            //{
            //    System.Reflection.Assembly a = result.CompiledAssembly;
            //}
        }

        private void AddReferences()
        {
            compilerParams.ReferencedAssemblies.Add("System.Drawing.dll");
            targetUnit.ReferencedAssemblies.Add("System.Drawing.dll");
        }
    }
}
